#include "main_window.h"

#include <unistd.h>

#include <QTimer>
#include <QDateTime>
#include <QMessageBox>
#include <QTabWidget>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    resize(640, 480);
    printf("Open main window, resize: %dx%d.\n", width(), height());

    create_menu_bar();
    create_status_bar();
    create_central();
}

MainWindow::~MainWindow()
{
}


// struct action_message_t {
//     QAction *act;
//     QString str;
// };

void MainWindow::create_menu_bar(void)
{
    menu_bar = this->menuBar();

    QMenu *file = menu_bar->addMenu(tr("&File"));
    QAction *exit = file->addAction(tr("E&xit"));
    connect(exit, &QAction::triggered, this, &MainWindow::close);

    QMenu *test = menu_bar->addMenu(tr("&Test"));
    QMenu *posb = test->addMenu(tr("&Progress on Status bar"));
    QAction *prepare_prog = posb->addAction(tr("&Prepare"));
    connect(prepare_prog, &QAction::triggered,
            this, &MainWindow::prepare_progress);
    QAction *run_prog = posb->addAction(tr("&Run"));
    connect(run_prog, &QAction::triggered, this, &MainWindow::run_progress);
    QAction *stop_prog = posb->addAction(tr("&Stop"));
    connect(stop_prog, &QAction::triggered, this, &MainWindow::stop_progress);

    QMenu *help = menu_bar->addMenu(tr("&Help"));
    QAction *about = help->addAction(tr("&About"));
    connect(about, &QAction::triggered, this, &MainWindow::about);
    // connect(about, SIGNAL(triggered()), this, SLOT(about()));
}

void MainWindow::prepare_progress(void)
{
    stop_progress();
    printf("progress prepare.\n");
    sb_progress_open("prepare...", 0, 0);
}

void MainWindow::run_progress_by_timer(void)
{
    int val = sb_progress_bar->value();
    if (val >= sb_progress_bar->maximum()) {
        stop_progress();
        return;
    }
    sb_progress_update(++val);
}

void MainWindow::run_progress(void)
{
    sb_progress_open("run", 0, 1000);
    printf("progress run(%d~%d).\n", sb_progress_bar->minimum(),
            sb_progress_bar->maximum());
    timer_run_progress->start(5);
}

void MainWindow::stop_progress(void)
{
    printf("progress stop.\n");
    sb_progress_close();
}

void MainWindow::display_message(QString str)
{
    printf("menu item triggered. message: %s\n", str.toStdString().data());
    QMessageBox::information(0, tr("Action"), str);
}

void MainWindow::about(void)
{
    printf("<About> triggered.\n");
    QMessageBox::information(0, tr("About"),
            tr("This is a Qt test program."));
}


void MainWindow::sb_update_time(void)
{
    QString time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    // printf("get time: %s.\n", time.toStdString().data());
    sb_time->setText(time);
}

void MainWindow::create_status_bar(void)
{
    status_bar = this->statusBar();

    sb_time = new QLabel();
    sb_update_time();
    status_bar->addPermanentWidget(sb_time);
    QTimer *timer = new QTimer();
    timer->start(1000);
    connect(timer, &QTimer::timeout, this, &MainWindow::sb_update_time);

    timer_run_progress = new QTimer();
    connect(timer_run_progress, &QTimer::timeout,
            this, &MainWindow::run_progress_by_timer);
    sb_progress_bar = new QProgressBar();
    sb_progress_bar->setFixedHeight(16);
    sb_progress_info = new QLabel();

    printf("status bar ready.\n");
    status_bar->showMessage("Ready", 3000);
}

void MainWindow::sb_progress_open(QString str, int min, int max)
{
    sb_progress_bar->setFormat("%p%");
    sb_progress_bar->setMinimum(min);
    sb_progress_bar->setMaximum(max);
    sb_progress_bar->setValue(0);
    status_bar->insertPermanentWidget(0, sb_progress_bar);
    sb_progress_bar->show();
    sb_progress_info->setText(str);
    status_bar->insertPermanentWidget(0, sb_progress_info);
    sb_progress_info->show();
}

void MainWindow::sb_progress_update(int val)
{
    sb_progress_bar->setValue(val);
}

void MainWindow::sb_progress_close(void)
{
    timer_run_progress->stop();
    status_bar->removeWidget(sb_progress_bar);
    status_bar->removeWidget(sb_progress_info);
}


#include "page2.h"
void MainWindow::create_central(void)
{
    printf("Create tab widget as central.\n");
    tab_widget = new QTabWidget();
    this->setCentralWidget(tab_widget);

    create_page1();

    TestPage2 *page2 = new TestPage2();
    tab_widget->addTab(page2, "&2-Image");
    tab_widget->setCurrentIndex(1);
}

